package com.ifast.integral.controller;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ifast.common.annotation.OperateRecord;
import com.ifast.common.base.AdminBaseController;
import com.ifast.common.utils.Result;
import com.ifast.integral.domain.IntegralConfig;
import com.ifast.integral.service.IntegralConfigService;
import com.ifast.sys.filter.SameUrlData;

/**
 * 积分配置
 * @author ShiQiang
 * @date 2019年4月9日10:05:14
 */
@RequestMapping("/integral/config/")
@Controller
public class IntegralConfigController extends AdminBaseController {
	
    private String preHtml = "lucky/integral/config/";
    @Autowired
    private IntegralConfigService integralConfigService;
     
    /**
     * 编辑规则
     * @param model
     * @param id 
     */
    @GetMapping("/edit")
    @RequiresPermissions("integral:config:edit")
    public String edit(Model model, String id) {
    	String topId = this.getUser().getDwCode();
    	if(StringUtils.isNotBlank(topId)){ 
    		model.addAttribute("integral",integralConfigService.selectOne(new EntityWrapper<IntegralConfig>(IntegralConfig.builder().dwCode(topId).build())));
    	}
        return  preHtml+"edit";
    } 
    
    /**
     * 保存积分配置
     * @param integral
     */
    @PostMapping("/save")
    @ResponseBody 
    @OperateRecord(OperateName = "保存积分配置",ModellName ="积分配置模块" )
    public Result<?> saveSubject(IntegralConfig integral) {
        try{ 
        	return integralConfigService.save(integral.setDeptId(this.getUser().getDeptId()).setDwCode(this.getUser().getDwCode()));
        }catch (Exception e){
            log.error(e.toString(),e);
            return Result.fail();
        } 
    } 
}